package com.fjnldx.mes.service.scheduling;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjnldx.mes.domain.domainReq.SchedulingLaborCapitalReq;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.pojo.SchedulingLaborCapital;
import com.fjnldx.mes.mapper.scheduling.SchedulingLaborCapitalMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class SchedulingLaborCapitalService {

    @Resource
    SchedulingLaborCapitalMapper mapper;


    /**
     * 新增劳资记录（单个
     *
     * @param data
     * @return
     */
    public Result addLaborCapital(SchedulingLaborCapital data) {
        String msg = data.saveCheck();
        if (!StringUtils.isEmpty(msg)) {
            return Result.error(msg);
        }
        //工序+工种+品种为唯一值，不能重复插入
        QueryWrapper<SchedulingLaborCapital> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gongxu", data.getGongxu());
        queryWrapper.eq("work_type", data.getWorkType());
        queryWrapper.eq("pz", data.getPz());
        Integer i = mapper.selectCount(queryWrapper);
        if (i > 0) {
            return Result.error("工序+工种+品种为唯一值，不能重复插入");
        }
        int insert = mapper.insert(data);
        return insert > 0 ? Result.success("新增成功") : Result.error("系统异常");
    }

    /**
     * 修改劳资记录
     *
     * @param data
     * @return
     */
    public Result modifyLaborCapital(SchedulingLaborCapital data) {
        String msg = data.updateCheck();
        if (!StringUtils.isEmpty(msg)) {
            return Result.error(msg);
        }
        int insert = mapper.updateById(data);
        return insert > 0 ? Result.success("更新成功") : Result.error("系统异常");
    }


    /**
     * 删除劳资记录
     *
     * @param array
     * @return
     */
    public Result deleteLaborCapital(String[] array) {
        if (null == array || array.length <= 0) {
            return Result.error("参数错误");
        }
        int i = mapper.deleteBatchIds(Arrays.stream(array).collect(Collectors.toList()));
        return i > 0 ? Result.success("删除成功") : Result.error("系统异常");
    }


    /**
     * 多条件查询劳资记录
     *
     * @param data
     * @return
     */
    public Result queryLaborCapital(SchedulingLaborCapitalReq data) {
        QueryWrapper<SchedulingLaborCapital> queryWrapper = new QueryWrapper<>();
        IPage<SchedulingLaborCapital> page = new Page<>();
        if (-1 == data.getSize()) {
            page.setSize(9999999);
            page.setPages(1);
        } else {
            page.setSize(data.getSize());
            page.setPages(data.getPage());
        }

        if (!StringUtils.isEmpty(data.getGongxu()) && !"null".equals(data.getGongxu())) {
            queryWrapper.eq("gongxu", data.getGongxu());
        }
        if (!StringUtils.isEmpty(data.getWorkType()) && !"null".equals(data.getWorkType())) {
            queryWrapper.eq("work_type", data.getWorkType());
        }
        if (!StringUtils.isEmpty(data.getPz()) && !"null".equals(data.getPz())) {
            queryWrapper.eq("pz", data.getPz());
        }
        if (!StringUtils.isEmpty(data.getLzxs()) && !"null".equals(data.getLzxs())) {
            queryWrapper.eq("lzxs", data.getLzxs());
        }

        IPage<SchedulingLaborCapital> result = mapper.selectPage(page, queryWrapper);
        Map<String, Object> res = new HashMap();
        res.put("dataList", result.getRecords());
        res.put("total", result.getTotal());
        return Result.success(res);

    }
}
